import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList; 
import oracle.jdbc.pool.OracleDataSource;
//this servlet is available from the homes main form
//it allows the user to view all the details of a house (i.e. sqftage, class, year, etc)
public class houseDetails extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		conn = new dbapi();
		// Create connection to Oracle
		conn.connect();
		if(!conn.connected)//if it can't connect, error message informs user
		{
			out.println("SQL ERROR CANNOT CONNECT TO DATABASE!<br>");
			out.println(conn.msg + "<br>");
			return;
		}
		//error handling for a null return object
		home h = conn.getHome(Integer
				.parseInt(request.getParameter("id")));
		if(h==null)
		{
			out.println("Error: null object returned<br>"+conn.msg);
			return;
		}
		
		out.println(htmlHeader.header);//prints the standard title bar
		ArrayList<String> appList = conn.getAppliancesForHouse(h.id);//gets the appliances in the array form (array is necessary because appliance numbers are dynamic)
		//home details are printed to the form
		out.println("<p weight=bold style=\"font-size:24pt;\">"+h.address+"</p>");
		out.println("<p weight=bold style=\"font-size:24pt;\">"+h.city+", "+h.state+"</p>");
		out.println("<p weight=bold style=\"font-size:24pt;\">"+h.zipcode+"</p>");
		out.println("Size: "+Integer.toString(h.sqft)+" sqft<br>");
		out.println("Floors: "+Integer.toString(h.numFloors)+" floors<br>");
		out.println("Bedrooms: "+Integer.toString(h.numBedrooms)+"<br>");
		out.println("Bathrooms: "+Integer.toString(h.numBathrooms)+"<br>");
		out.println("Land Size: "+Integer.toString(h.numFloors)+" acres<br>");
		out.println("Class: "+h.homeClass+"<br>");
		out.println("Year Made: "+Integer.toString(h.year)+"<br><br>");
		
		//if appliance list is greater than 0
		if(appList.size() > 0)
		{
			out.println("Appliances: <br>");//print "Appliances:"
			for(int i=0;i<appList.size();i++)//print out each appliance using a for loop to move through the array
			{
				out.println(appList.get(i) + "<br>");
			}
		}
		out.println("<br><br>");
		out.println("<a href=\"addAppliance?id="+h.id+"\">Add Appliance</a>");//hotlink to move to the add appliance servlet
		if(!conn.isHouseOnMarket(h.id))//if house is not on the market, a hotlink for the add to market servlet is displayed
			out.println("&nbsp&nbsp<a href=\"addMarket?id="+h.id+"\">Add to Market</a><br>");
	}
}
